package com.hades.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ContextServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletConfig().getServletContext();
        System.out.println(context);
        String username = context.getInitParameter("username");
        String password = context.getInitParameter("password");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        System.out.println("当前工程路径:"+context.getContextPath());// /06_servlet
        /*
        斜杠指:http://ip:port/工程名/ <br/>
        而要访问工程下的相对路径，"/css"  "/imgs/1.jpg"这种就可以了
         */
        System.out.println("工程部署的路径是:"+context.getRealPath("/")); // E:\MySQL\JDBC\jdbc-code\out\artifacts\06_servlet_war_exploded\
    }
}
